package com.his.struts.action;

import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.ResultSet;
import java.util.List;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.io.CopyUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.apache.struts.upload.FormFile;

import com.his.struts.dao.users.UsersDao;
import com.his.struts.dao.users.UsersImp;
import com.his.struts.form.DoctorForm;
import com.his.struts.form.RyForm;
import com.his.struts.form.UserForm;
import com.his.struts.util.PageUtil;
/**
 * 医生功能类
 * @author liuyan
 *
 */
public class DoctorAction extends DispatchAction {
	UsersDao dao = new UsersImp();

	/**
	 * Method doctorLogin
	 * 医生登陆
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	public ActionForward doctorLogin(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		//医生form
		DoctorForm doc = (DoctorForm) form;
		//得到医生名和密码
		String name = doc.getDocname();
		String pass = doc.getDocpass();
		String cook =request.getParameter("cook1");
		//得到session
		HttpSession session = request.getSession(false);
		ActionForward forword = null;
		//判断登陆是否成功
		boolean ok = dao.docLogin(name, pass);
		if (ok) {
			if(cook != null){
				String uname = Base64.encode(name, "utf-8");
				String upass = Base64.encode(pass, "utf-8");
				Cookie c = new Cookie("docCookieName",uname);
				Cookie c1 = new Cookie("docCookiePass",upass);
				c.setMaxAge(10000);
				c1.setMaxAge(10000);
				response.addCookie(c);
				response.addCookie(c1);
			}else{
				Cookie[] c = request.getCookies();
				for (Cookie cookie : c) {
					if("docCookieName".equals(cookie.getName())){
						cookie.setValue("");
					}
					if("docCookiePass".equals(cookie.getName())){
						cookie.setValue("");
					}
				}
			}
			request.getSession().setAttribute("loginError", "");
			//设置医生到session
			session.setAttribute("ysname", name);
			forword = mapping.findForward("loginSuccess");
		} else {
			request.getSession().setAttribute("loginError", "请输入正确的用户名或密码！！！");
			forword = mapping.findForward("loginFail");
		}

		return forword;
	}
	/**
	 * Method doctorIndex
	 * 医生登陆首页
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	public ActionForward doctorIndex(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ActionForward forword = null;
		//得到医生名
		HttpSession session = request.getSession();
		// 取出医生名字
		String ysname = (String)session.getAttribute("ysname");
		// 医生首页
		DoctorForm docinfo =  dao.docIndex(ysname);
		/*
		 * 如果医生信息不为空
		 * 把信息设置到session中 跳医生首页
		 * 如果信息为空
		 * 跳到登陆页
		 */
		if(docinfo != null){
			session.setAttribute("docinfo", docinfo);
			forword = mapping.findForward("doctorIndex");
		}else{
			forword = mapping.findForward("login");
		}
		return forword;
	}
	
	
	/**
	 * method getAllDoc 得到所有医生
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward getAllDoc(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// 判断医生id是否为空
		String id = request.getParameter("did");
		if (id == null) {
			id = "";
		} else {
			int did = Integer.parseInt(id);
			// 删除医生
			dao.delDoc(did);
			request.setAttribute("message", "删除成功");
		}
		
		String sql = "doctorinfo left join kebie on kb_id=attend order by did desc";
		// 调用分页类 
		ResultSet rs = PageUtil.getPage(sql, 10, request);
		if(rs != null){
			// 封装
			List<DoctorForm> dlist = dao.getDocBean(rs);
			// 数据放到request中
			request.setAttribute("dlist", dlist);
		}
		
		return mapping.findForward("allDoc");
	}

	/**
	 * method addDoc 添加医生
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	public ActionForward addDoc(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		// 医生form
		DoctorForm dform = (DoctorForm) form;
		// 添加医生并得到所有
		dao.addDoc(dform);
		String sql = "doctorinfo left join kebie on kb_id=attend order by did desc";
		// 调用分页类 
		ResultSet rs = PageUtil.getPage(sql, 10, request);
		if(rs != null){
			// 封装
			List<DoctorForm> dlist = dao.getDocBean(rs);
			
			// 数据放到request中
			request.setAttribute("dlist", dlist);
		}
		request.setAttribute("message", "添加成功");
		return mapping.findForward("allDoc");
	}

	/**
	 * method getAllDoc 更新医生信息
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	public ActionForward updateDoc(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		// 医生form
		DoctorForm dform = (DoctorForm) form;
		// 更新医生信息并显示所有
		dao.updateDoc(dform);

		String sql = "doctorinfo left join kebie on kb_id=attend order by did desc";
		// 调用分页类 
		ResultSet rs = PageUtil.getPage(sql, 10, request);
		if(rs != null){
			// 封装
			List<DoctorForm> dlist = dao.getDocBean(rs);
			// 数据放到request中
			request.setAttribute("dlist", dlist);
		}
		request.setAttribute("message", "修改成功");
		return mapping.findForward("allDoc");
}
	
	/** 
	 * Method updateInfo 更新医生资料
	 * @param mapping 配置文件
	 * @param form 表单数据
	 * @param request 网页请求
	 * @param response 响应
	 * @return ActionForward 用户资料页
	 */
	public ActionForward updateInfo(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		DoctorForm doctorForm = (DoctorForm) form;
		DoctorForm docinfo = (DoctorForm)request.getSession().getAttribute("docinfo");
		// 得到用户名
		String doctorName = docinfo.getDocname();
		// 得到头像
		FormFile file = doctorForm.getPic();
		if(file != null){
			// 得到写出途径
			String outPath = request.getRealPath("images/touxiang/doctor")+"\\"+doctorForm.getDocpicture();
			// 创建写出流
			OutputStream output = new FileOutputStream(outPath);
			// 创建写入流
			InputStream input = file.getInputStream();
			// 把写入流里的文件 copy 到写出流中 且写入
			CopyUtils.copy(input, output);
			input.close();
			output.close();
		}	
		// 把用户修改到信息更新到数据库
		dao.updateDoctorInfo(doctorForm);
		// 重新读取用户信息
		docinfo =  dao.docIndex(doctorName);
		// 把用户最新信息更新到session中
		request.getSession().setAttribute("docinfo", docinfo);
		request.getSession().setAttribute("message", "更新成功，请刷新查看头像");
		return mapping.findForward("docInfo");
	}
}